begintownscript;

variables;
short bmessage,leadchar,pcs,count,rctx,rcty,terr_hght;
string custom_m;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	change_outdoor_location(0,0,37,4);
	if(get_flag(76,0) == 1){
		set_terrain(9,22,375);
		set_terrain(23,22,374);
		set_terrain(10,22,440);
		set_terrain(22,22,440);
		set_terrain(5,16,137);
	}
	if(get_flag(75,0) >= 2){
		erase_char(6);
		rctx = 10;
		while(rctx <= 22){
			rcty = 10;
			while(rcty <= 22){
				set_height(rctx,rcty,5);
				if(get_terrain(rctx,rcty) == 122)
					set_terrain(rctx,rcty,0);

				rcty = (rcty + 1);
			}
			rctx = (rctx + 1);
		}
	}
	if(get_flag(75,0) < 2){
		set_total_visibility(0);
		leadchar = 0;
		while(char_ok(leadchar) == 0){
			leadchar = (leadchar + 1);
		}
		set_character_facing(1000,0);
		relocate_character(leadchar,16,16);
		force_view_center(16,16);
		set_character_facing(leadchar,get_ran(1,0,7));
		pcs = (leadchar + 1);
		count = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE){
				if(count == 0)
					relocate_character(pcs,18,17);
				if(count == 1)
					relocate_character(pcs,15,17);
				if(count == 2)
					relocate_character(pcs,17,18);
				set_character_facing(pcs,get_ran(1,0,7));

				count = (count + 1);
			}
			pcs = (pcs + 1);
		}

		force_instant_terrain_redraw();
		play_sound(5);
		play_sound(65);
		pause(2);

		while(get_height(16,16) < 5){
			rctx = 13;
			while(rctx <= 19){
				rcty = 13;
				while(rcty <= 19){
					terr_hght = get_height(rctx,rcty);
					if(terr_hght < 5){
set_height(rctx,rcty,(terr_hght + 1));
					}

					rcty = (rcty + 1);
				}
				rctx = (rctx + 1);
			}

			force_instant_terrain_redraw();
			play_sound(65);
			pause(2);
		}

		reset_dialog();
		add_dialog_str(0,"Wow...  That was a shock.  No real damage done, but still shocking to stop so abruptly.  A bit nauseating, but--",0);
		add_dialog_str(1," ",0);
		add_dialog_str(2,"The Immortal!  Damn, he's still here!",30);
		add_dialog_choice(0,"Get him!");
		bmessage = run_dialog(0);

		relocate_character(6,4,16);
		set_character_facing(6,6);
		put_boom_on_space(16,13,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);

		force_view_center(8,16);
		set_total_visibility(1);
		put_boom_on_space(4,16,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		pause(2);

		text_bubble_on_char(6,"Take this!");
		force_instant_terrain_redraw();
		pause(4);

		set_character_pose(6,1);
		force_instant_terrain_redraw();
		play_sound(71);
		pause(1);

		set_terrain(5,16,137);
		force_instant_terrain_redraw();
		play_sound(108);
		pause(5);

		set_character_pose(6,0);
		text_bubble_on_char(6,"");
		force_instant_terrain_redraw();
		pause(2);

		set_total_visibility(0);
		force_view_center(16,16);
		force_instant_terrain_redraw();
		pause(2);

		rctx = 10;
		while(rctx <= 22){
			rcty = 10;
			while(rcty <= 22){
				if(get_terrain(rctx,rcty) == 122){
	set_terrain(rctx,rcty,0);
	if(get_ran(1,0,2) == 2)
		put_effect_on_space(rctx,rcty,10,1,1);
				}

				rcty = (rcty + 1);
			}
			rctx = (rctx + 1);
		}
		force_instant_terrain_redraw();
		run_animation_sound(163);
		pause(2);

		reset_dialog();
		add_dialog_str(0,"...",0);
		add_dialog_choice(0,"Well.  This looks like the end.");
		bmessage = run_dialog(0);

		teleport_party(7,13,0);
		set_character_facing(1000,4);
		force_instant_terrain_redraw();
		pause(3);

		set_character_facing(6,0);
		force_instant_terrain_redraw();
		pause(2);

		text_bubble_on_char(6,"What!");
		force_instant_terrain_redraw();
		pause(15);

		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Katothen...");
		force_instant_terrain_redraw();
		pause(18);

		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"He's quick.");
		force_instant_terrain_redraw();
		pause(15);

		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"So be it.");
		force_instant_terrain_redraw();
		pause(15);

		text_bubble_on_char(6,"");
		erase_char(6);
		put_boom_on_space(4,16,2,0);
		force_instant_terrain_redraw();
		run_animation_sound(10);
		pause(6);

		reset_dialog();
		if(get_flag(24,1) == 0)
			add_dialog_str(0,"He mentioned someone named Katothen...  Is he talking about that mysterious figure?",0);
		else
			add_dialog_str(0,"He mentioned someone named Katothen...  Is that what _K_ stands for?",0);
		if(party_size() == 1){
			add_dialog_str(1,"Anyway, my life seems to be intact, but at what cost.  The transportation pit has just become completely unusable, preventing me from returning.",0);
			add_dialog_str(2,"And _The Immortal_...  Odds are, I will have to fight him at least once more before all of this has settled.  Here's hoping he doesn't take me by such surprise next time.",0);
			add_dialog_choice(0,"Now...  where am I?");
		}
		if(party_size() > 1){
			add_dialog_str(1,"Anyway, our lives seem to be intact, but at what cost.  The transportation pit has just become completely unusable, preventing us from returning.",0);
			add_dialog_str(2,"And _The Immortal_...  Odds are, we will have to fight him at least once more before all of this has settled.  Here's hoping he doesn't take us by such surprise next time.",0);
			add_dialog_choice(0,"Now...  where are we?");
		}
		bmessage = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"But these surroundings...  It's incredible.  The cave here is incredibly expansive.  There are fluorescent fungi lining the ceiling, and while they are sparse, they're enough to see with.",0);
		if(party_size() == 1)
			add_dialog_str(1,"Wherever the brigand lair is, however, it's not in the immediate area.  That gives me the dubious pleasure of spelunking to go looking for it.",0);
		if(party_size() > 1)
			add_dialog_str(1,"Wherever the brigand lair is, however, it's not in the immediate area.  That gives us the dubious pleasure of spelunking to go looking for it.",0);
		add_dialog_choice(0,"This should be fun.");
		bmessage = run_dialog(1);

		set_flag(75,0,2);
		toggle_quest(2,0);
		award_party_xp(500,25);
		award_party_xp(500,25);
	}
	force_instant_terrain_redraw();

break;

beginstate START_STATE;
break;

beginstate EXIT_STATE;
break;

break;

beginstate 10;

	reset_dialog_preset_options(2);
	bmessage = run_dialog(0);
	if(bmessage == 1)
		end();
	play_sound(99);
	if(get_flag(76,0) == 0){
		set_terrain(9,22,375);
		set_terrain(23,22,374);
		set_terrain(10,22,440);
		set_terrain(22,22,440);
		set_flag(76,0,1);
	}
	else{
		set_terrain(9,22,374);
		set_terrain(23,22,375);
		set_terrain(10,22,441);
		set_terrain(22,22,441);
		set_flag(76,0,0);
	}
	force_instant_terrain_redraw();
	play_sound(94);

break;

beginstate 31;

	if(get_flag(76,0) == 0)
		message_dialog("These strange, glass tubes have to have some purpose.  The green tube is lit by some magical force and the red one is not...  Hopefully, that means _there is nothing dangerous here_ and nothing more.","");
	else
		message_dialog("These strange, glass tubes have to have some purpose.  The red tube is lit by some magical force and the green one is not...  Hopefully, that means _there is nothing dangerous here_ and nothing more.","");

break;

